/*  lexeme.cpp

Lexeme class for handing individual Lexemes
and their data.

Nicholas Green
Lexical Analyzer
*/

#include "lexeme.h"
#include <stdio.h>
#include <string.h>
#include <algorithm>
using std::string;

Lexeme::Lexeme()
  : iVal(0), rVal(0), line(0),
  leftPtr(NULL), rightPtr(NULL)
{
  tVal = '\0';
#ifdef _WIN32
  //Windows apparently doesn't
  //go ahead and allocate chars
  //in a string until you actually
  //use it.
  this->sVal = '\0';
#else
  this->sVal[0] = '\0';
#endif

}
/*
const char* tVal;
int iVal;
double rVal;
std::string sVal;
int line;
Lexeme* leftPtr;
Lexeme* rightPtr
*/
//Lexeme::Lexeme(const Lexeme& copy)
//  : iVal(copy.iVal), tVal(copy.tVal) , rVal(copy.rVal), sVal(copy.sVal), line(copy.line)
//{
//  if (copy.leftPtr) { leftPtr = new Lexeme(*Lexeme.leftPtr); }
//  if (Lexeme.rightPtr) {
//    try {
//      rightPtr = new Lexeme(*Lexeme.rightPtr);
//    } catch (...) {
//      delete leftPtr;
//      throw;
//    }
//  }
//}

Lexeme::Lexeme(const char* t, int l)
  : tVal(t), iVal(0), rVal(0), line(l),
  leftPtr(NULL), rightPtr(NULL)
{

#ifdef _WIN32
  this->sVal = '\0';
#else
  this->sVal[0] = '\0';
#endif
}

Lexeme::Lexeme(const char* t, int l, Lexeme* left, Lexeme* right)
  : tVal(t), iVal(0), rVal(0), line(l),
  leftPtr(left), rightPtr(right)
{
  //#ifdef _WIN32
  //  this->sVal = '\0';
  //#else
  this->sVal[0] = '\0';
  //#endif
}

Lexeme::Lexeme(const char* t, int i, int l)
  : tVal(t), iVal(i), rVal(0), line(l),
  leftPtr(NULL), rightPtr(NULL)
{
  #ifdef _WIN32
    sVal = '\0';
  #else
  sVal[0] = '\0';
  #endif
}

Lexeme::Lexeme(const char* t, int i, int l,
  Lexeme* left, Lexeme* right)
  : tVal(t), iVal(i), rVal(0), line(l),
  leftPtr(left), rightPtr(right)
{
#ifdef _WIN32
  sVal = '\0';
#else
  sVal[0] = '\0';
#endif
}

Lexeme::Lexeme(const char* t, double d, int l)
  : tVal(t), iVal(0), rVal(d), line(l),
  leftPtr(NULL), rightPtr(NULL)
{
#ifdef _WIN32
  sVal = '\0';
#else
  sVal[0] = '\0';
#endif
}

Lexeme::Lexeme(const char* t, double d, int l,
  Lexeme* left, Lexeme* right)
  : tVal(t), iVal(0), rVal(d), line(l),
  leftPtr(left), rightPtr(right)
{
#ifdef _WIN32
  sVal = '\0';
#else
  sVal[0] = '\0';
#endif
}

Lexeme::Lexeme(const char* t, string s, int l)
  : tVal(t), iVal(0), rVal(0), sVal(s), line(l),
  leftPtr(NULL), rightPtr(NULL){}

Lexeme::Lexeme(const char* t, string s, int l,
  Lexeme* left, Lexeme* right)
  : tVal(t), iVal(0), rVal(0), sVal(s), line(l),
  leftPtr(left), rightPtr(right) { }

//Print out the Lexeme information
void Lexeme::print(void)
{
  if(strcmp(tVal,"COMMENT") == 1)
  {
    printf("%s ", tVal);
    if (!sVal.empty())
    {
      for (size_t i = 0; i < sVal.length(); ++i)
      {
        if (sVal[i] == '\r')
        {
          i++;
          printf("\n\t");
        }
        else
          printf("%c", sVal[i]);
      }
    }
    printf("\n");
  }
}

Lexeme::~Lexeme(void)
{
  //delete leftPtr;
  //leftPtr = NULL;
  //delete rightPtr;
  //rightPtr = NULL;
}
